# !/usr/bin/python3
# _*_coding: utf-8_*_

import re
import json
import uuid


class BaiduEntry_Processor(object):

    def __init__(self):
        self._labels = {}

    def process(self, filepath, outputpath):
        data = []
        with open(filepath, "r") as f:
            lines = f.readlines()
            for line in lines:
                item = {}
                infos = line.split('\t')
                item['label'] = infos[0]
                attr = eval(infos[1])
                item_id = "xiaogu-cogik" + str(uuid.uuid3(uuid.NAMESPACE_OID,
                                                          infos[0]))
                item['id'] = item_id
                attr_ = {}
                for key, value in attr.items():
                    if key is None or value is None:
                        continue
                    if "<dd" in value:
                        value_ = re.sub(r'<[^>]+>', "", value)
                    else:
                        value_ = value
                    key_ = re.sub('\s', '', key)
                    value_ = re.sub("(^\s*)|(\s*$)", "", value_)
                    value_ = re.sub(r"[\n]+", "", value_)
                    attr_[key_] = value_
                item['attr'] = attr_
                data.append(item)
            with open(outputpath, 'w') as f:
                json.dump({'data': data}, f)
